package com.cmnit.bean;

import com.cmnit.utils.MD5Utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class DangerousTruckPostBean {
    private String ak;
    private String ts;
    private String sk;
    private String sn;
    private String page;
    private String limit;

    public DangerousTruckPostBean() {
    }

    public String getAk() {
        return ak;
    }

    public void setAk(String ak) {
        this.ak = ak;
    }

    public String getTs() {
        return ts;
    }

    public void setTs(Long ts) {
        this.ts = String.valueOf(ts);
    }

    public String getSk() {
        return sk;
    }

    public void setSk(String sk) {
        this.sk = sk;
    }

    public String getSn() {
        return sn;
    }

    public void setSn() {
        // 获取认证字符串
        String enBaseString = this.basicString();
        System.out.println("认证字符串：" + enBaseString);

        // 转换URL格式编码
        try {
            enBaseString = URLEncoder.encode(enBaseString, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 认证字符串MD5后得到SN码
        this.sn = MD5Utils.string2Md5(enBaseString);
        System.out.println("sn:" + sn);
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    /**
     * 获取指定字段顺序的json字符串
     *
     * @return json
     */
    private String psJson() {
        // 格式：{"page":"1","limit":"300"}
        return "{\"page\":\"" + this.page + "\",\"limit\":\"" + this.limit + "\"}";
    }

    /**
     * 获取认证字符串
     *
     * @return 认证字符串
     */
    public String basicString() {
        return "ak=" + this.getAk() + "ts=" + this.getTs() + "sk=" + this.getSk() + "ps=" + this.psJson();
    }

    /**
     * 获取指定字段顺序的KV
     *
     * @return KV
     */
    public String mkKVPair() {
        // 获取sn
        this.setSn();
        // 格式：ak=360000&ts=1657002087524&sk=Ctfo7890&ps=%7B%22page%22%3A%221%22%2C%22limit%22%3A%22300%22%7D&sn=15795b5807df14f45d0f0a5130998105
        return "ak=" + this.ak + "&ts=" + this.ts + "&sk=" + this.sk + "&ps=" + this.psJson() + "&sn=" + this.sn;
    }
}
